package cn.ffcs.organ.web;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.organ.model.OrgRelaPerson;
import cn.ffcs.organ.model.Organization;
import cn.ffcs.organ.model.Person;
import cn.ffcs.organ.service.IOrgRelaPersonService;
import cn.ffcs.organ.service.IOrganizationService;
import cn.ffcs.organ.service.IPersonService;
import cn.ffcs.security.model.User;
import cn.ffcs.util.MyList;
import cn.ffcs.util.context.ApplicationContextUtil;

/**
 * 
 * 人员选择<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class SelectPersonAction<P extends Person> extends BaseAction<P>
{
    /** 网络标识 */
    private static final long serialVersionUID = -6609199907793393385L;
    /** 人员service* */
    private IOrganizationService<Organization> organizationService;
    /** 部门人员service* */
    private IOrgRelaPersonService<OrgRelaPerson> orgRelaPersonService;
    /** 人员service* */
    private IPersonService<P> personService;
    /** 公司列表* */
    private List<Organization> corps;
    /** 部门列表* */
    private List<Organization> depts;
    /** 人员列表* */
    private List<Person> persons;
    
    /** 返回对象* */
    private String returnObj;
    /** 返回对象* */
    private String returnObjName;
    /** 公司ID* */
    private String corpID;
    /** 取数标识，CORP为取本公司数据* */
    private String flag;

    /**
     * 数据初始化
     */
    private void initData()
    {
        if (StringUtils.isNotEmpty(flag) && "CORP".equals(flag))
        {
            User currUser = ApplicationContextUtil.getUserContextObject()
                    .getCurrUser();
            Criterion cond = Restrictions.eq("person", currUser.getPerson());
            List<OrgRelaPerson> orgRelaPersons = orgRelaPersonService
                    .findBy(cond);
            if (orgRelaPersons != null && orgRelaPersons.size() > 0)
            {
                OrgRelaPerson orgRelaPerson = orgRelaPersons.get(0);
                // this.corpID = orgRelaPerson.getRoot();
            }
        }
        if (StringUtils.isEmpty(corpID))
        {
            corps = organizationService.findAllCorps();
        }
        else
        {
            Organization corp = organizationService.findById(Integer
                    .parseInt(corpID));
            corps = new ArrayList<Organization>();
            corps.add(corp);
        }
    }
    
    /**
     * 人员查询
     * @return
     */
    public String findPersonByCond()
    {
        ActionContext ctx = ActionContext.getContext();
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        
        try
        {
            corpID=new String(corpID.getBytes("iso8859-1"), "utf-8");
            if(corpID!=null&&!"".equals(corpID.trim())){
                Criterion cond = Restrictions.like("name", "%"+corpID.trim()+"%");
                persons = (List<Person>)personService.findBy(cond);
            }
        }
        catch (UnsupportedEncodingException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String xml=MyList.getAll(persons);
        response.setContentType("text/xml;charset=GBK");//页面文件头格式
        try{
            response.getWriter().write(xml);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 公司选择
     * @return
     */
    public String selectCorp(){
        if(corpID!=null&&!"".equals(corpID)){
            
        }else{
            ///公司id为空时，取所有公司数据
            Criterion cond1 = Restrictions.eq("superior", "0");
            Criterion cond2 = Restrictions.eq("orgType", "CORP");
            Criterion cond = Restrictions.and(cond1, cond2);
            corps = organizationService.findBy(cond);
        }
        return SUCCESS;
    }

    /**
     * 公司选择
     * @return
     */
    public String selectProdCorp(){
    	String codeId=ApplicationContextUtil.getContextObject("codeId");
    	if(StringUtils.isNotBlank(codeId)){
    		Criterion cond1 = Restrictions.eq("superior", "0");
            Criterion cond2 = Restrictions.eq("codeID", codeId);
            Criterion cond = Restrictions.and(cond1, cond2);
            corps = organizationService.findBy(cond);
    	}else{
            ///公司id为空时，取所有公司数据
            Criterion cond1 = Restrictions.eq("superior", "0");
            Criterion cond2 = Restrictions.eq("orgType", "CORP");
            Criterion cond = Restrictions.and(cond1, cond2);
            corps = organizationService.findBy(cond);
    	}
        return SUCCESS;
    }
    /**
     * 获取部门
     * @return
     */
    public String findOrganization(){
        ActionContext ctx = ActionContext.getContext();
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        HttpServletRequest request =    (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
        String codeId=ApplicationContextUtil.getContextObject("codeId");
//        if(corpID!=null&&!"".equals(corpID)){
//            Criterion cond = Restrictions.eq("superior", corpID);
//            depts = organizationService.findBy(cond);
//        }
        
        if(StringUtils.isNotBlank(codeId)&&StringUtils.isNotBlank(corpID)){
        	Criterion cond1 = Restrictions.eq("superior", corpID);
        	Criterion cond2 = Restrictions.eq("codeID", codeId);
            Criterion cond = Restrictions.and(cond1, cond2);
            depts = organizationService.findBy(cond);
        }else if(StringUtils.isNotBlank(corpID)){
        	Criterion cond = Restrictions.eq("superior", corpID);
            depts = organizationService.findBy(cond);
        }
        
        String xml=MyList.getAll(depts);
        response.setContentType("text/xml;charset=gb2312");//页面文件头格式
        try{
        response.getWriter().write(xml);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 
     * 公司选择
     * @return
     */
    public String findAllOrgById(String orgId){
        if(orgId!=null&&!"".equals(orgId)){
            Criterion cond = Restrictions.eq("superior", orgId);
            depts = organizationService.findBy(cond);
        }
        return SUCCESS;
    }
    
    /**
     * 人员选择
     * @return
     */
    public String findPersonByOrg()
    {
        ActionContext ctx = ActionContext.getContext();
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
        if(corpID!=null&&!"".equals(corpID)){
            persons= orgRelaPersonService.findByPageByOrg(corpID);
        }
        String xml=MyList.getAll(persons);
        response.setContentType("text/xml;charset=GBK");//页面文件头格式
        try{
        response.getWriter().write(xml);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return the organizationService
     */
    public IOrganizationService<Organization> getOrganizationService()
    {
        return organizationService;
    }

    /**
     * @param organizationService the organizationService to set
     */
    public void setOrganizationService(
            IOrganizationService<Organization> organizationService)
    {
        this.organizationService = organizationService;
    }

    /**
     * @return the orgRelaPersonService
     */
    public IOrgRelaPersonService<OrgRelaPerson> getOrgRelaPersonService()
    {
        return orgRelaPersonService;
    }

    /**
     * @param orgRelaPersonService the orgRelaPersonService to set
     */
    public void setOrgRelaPersonService(
            IOrgRelaPersonService<OrgRelaPerson> orgRelaPersonService)
    {
        this.orgRelaPersonService = orgRelaPersonService;
    }

    /**
     * @return the personService
     */
    public IPersonService<P> getPersonService()
    {
        return personService;
    }

    /**
     * @param personService the personService to set
     */
    public void setPersonService(IPersonService<P> personService)
    {
        this.personService = personService;
    }

    /**
     * @return the corps
     */
    public List<Organization> getCorps()
    {
        return corps;
    }

    /**
     * @param corps the corps to set
     */
    public void setCorps(List<Organization> corps)
    {
        this.corps = corps;
    }

    /**
     * @return the depts
     */
    public List<Organization> getDepts()
    {
        return depts;
    }

    /**
     * @param depts the depts to set
     */
    public void setDepts(List<Organization> depts)
    {
        this.depts = depts;
    }

    /**
     * @return the returnObj
     */
    public String getReturnObj()
    {
        return returnObj;
    }

    /**
     * @param returnObj the returnObj to set
     */
    public void setReturnObj(String returnObj)
    {
        this.returnObj = returnObj;
    }

    /**
     * @return the returnObjName
     */
    public String getReturnObjName()
    {
        return returnObjName;
    }

    /**
     * @param returnObjName the returnObjName to set
     */
    public void setReturnObjName(String returnObjName)
    {
        this.returnObjName = returnObjName;
    }

    /**
     * @return the corpID
     */
    public String getCorpID()
    {
        return corpID;
    }

    /**
     * @param corpID the corpID to set
     */
    public void setCorpID(String corpID)
    {
        this.corpID = corpID;
    }

    /**
     * @return the flag
     */
    public String getFlag()
    {
        return flag;
    }

    /**
     * @param flag the flag to set
     */
    public void setFlag(String flag)
    {
        this.flag = flag;
    }

    public List<Person> getPersons()
    {
        return persons;
    }

    public void setPersons(List<Person> persons)
    {
        this.persons = persons;
    }


}
